package uiLayer;
import java.util.Scanner;
import controlLayer.*;
import modelLayer.*;
import java.util.ArrayList;

public class OrderUI {
    private CustomerUI custUI;
    private EmployeeUI empUI;
    private ProductUI prodUI;
    private OrderCtr ordCtr;
    private Customer cust;
    private Employee emp;
    int ccpr;
    int ecpr;
    
    public OrderUI() {
        custUI = new CustomerUI();
        empUI = new EmployeeUI();
        prodUI = new ProductUI();
        ordCtr = new OrderCtr();
    }
    
    public void orderMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeOrderMenu();
            
            switch(choice) {
            case 1 : createOrder();
                    stop = true;
                break;
            case 2 : findOrder();
                break;
            case 3 : cancelOrder();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeOrderMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Order Menu ***");
        System.out.println("1. Create order ");
        System.out.println("2. Find order ");
        System.out.println("3. Cancel order ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    private void createOrder() {
        ccpr = custUI.inputCprNo();
        cust = (ordCtr.findCustomer(ccpr));
        if(cust != null) {
            ecpr = empUI.inputCprNo();
            emp = (ordCtr.findEmployee(ecpr));
            if(emp != null) {
                ordCtr.createOrder(ccpr, ecpr);
            }   
            else {
                System.out.println("No employee was found with the specified id");
            }
        }
        else {
            System.out.println("No customer was found with the specified id");
        }
        MainMenuUI.pause();
    }
    
    private void findOrder() {
        int id = inputId();
        Order ord = (ordCtr.findOrder(id));
        if (ord != null) {
            System.out.println();
            showDate(ord.getDate());
            showStatus(ord.getStatus());
            System.out.println();
            Customer customer = ord.getCustomer();
            System.out.println("Customer name : " + customer.getName());
            System.out.println("Customer CPR numer : " + customer.getCpr());
            System.out.println();
            Employee employee = ord.getEmployee();
            System.out.println("Employee name : " + employee.getName());
            System.out.println("Eployee CPR : " + employee.getCpr());
            System.out.println();
            System.out.println("Name\t\tbarcode\t\tunit price\tquantity");
            for(OrderLine orderLine : ord.getOrderLines()) {
                Product product = orderLine.getProduct();
                System.out.println(product.getName() + "\t\t"+ product.getBarcode() + "\t\t" + product.getPrice() + "\t\t" + orderLine.getQuantity());
                System.out.println();
            }
            showTotalPrice(ord.getTotalPrice());
        }
        else {
            System.out.println("No order was found with the specified id.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void cancelOrder() {
        Scanner keyboard = new Scanner(System.in);
        int id = inputId();
        Order ord = (ordCtr.findOrder(id));
        boolean stop = false;
        if (ord != null) {
            System.out.println();
            showDate(ord.getDate());
            showStatus(ord.getStatus());
            showTotalPrice(ord.getTotalPrice());
            
            System.out.println();
            System.out.println("1. Cancel order");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                    case 1 : ord.setStatus("Cancelled");
                             System.out.println();
                             System.out.println("The order was cancelled");
                             System.out.println();
                             ArrayList<OrderLine> orderLines = ord.getOrderLines();
                             for(OrderLine ol : orderLines) {
                                 Product prod = ol.getProduct();
                                 prod.setInStock(prod.getInStock()+ol.getQuantity());
                             }
                             MainMenuUI.pause();
                        break;
                    case 0 : stop = true;
                        break;
                }
            }
        }
        else {
            System.out.println("No order was found with the specified id.");
        }
    }
    
    private int inputId() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an id (numeric value) for the order : ");
        int oid = keyboard.nextInt();
        return oid;
    }
    
    private String inputDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the order date (dd/mm/yyyy) : ");
        String date = keyboard.nextLine();
        return date;
    }
    
    private String inputPeriod() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the loan period (number of days): ");
        String period = keyboard.nextLine();
        return period;
    }
    
    public int inputQuantity() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the quantity : ");
        int quantity = keyboard.nextInt();
        return quantity;
    }
    
    private void showDate(String date)
    {
        System.out.println("Order date : " + date);
    }
    
    private void showStatus(String status)
    { 
        System.out.println("Order status : " + status);
    }
    
    private void showTotalPrice(double totalPrice)
    { 
        System.out.println("Total price : " + totalPrice);
    }
         
}
